package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.entity.CaTimes;
import com.dingreading.cloud.soms.service.CaTimesService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/front/caTimes")
@Api(tags = "门店-排课时间段管理")
public class FrontCaTimesController {

    @Resource
    private CaTimesService caTimesService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "时间名称") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<CaTimes> page = caTimesService.pageList(pageUtil, storeUid, keyword, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "根据门店uid获取时间段")
    @PostMapping(value = "/getTimes", produces = "application/json; charset=UTF-8")
    public R<Object> getTimes(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "时间名称") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        List<CaTimes> list = caTimesService.getByStoreUid(storeUid, keyword, enabled);
        return R.ok(list);
    }

    @ApiOperation(value = "获取时间段详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        CaTimes data = caTimesService.getBean(storeUid, id);
        return R.ok(data);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "创建/编辑时间段")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "开始时间 HH:mm") @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(value = "结束时间 HH:mm") @RequestParam(value = "endTime") String endTime
    ) throws Exception {
        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime))
            return R.fail("请填写开始时间或结束时间");

        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date begin = format.parse(beginTime);
        Date end = format.parse(endTime);
        if (begin.getTime() >= end.getTime())
            return R.fail("结束时间必须大于开始时间");

        boolean b = caTimesService.haveTime(storeUid, id, beginTime, endTime);
        if (b) return R.fail("时间段重复了");

        CaTimes data = null;
        if (null == id || id <= 0) {
            // 创建
            data = new CaTimes();
            data.setCorpUid(corpUid);
            data.setStoreUid(storeUid);
            data.setStoreName(storeName);
            data.setEnabled(1);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
        } else {
            // 编辑
            data = caTimesService.getById(id);
            if (null == data) return R.fail("时间段获取失败");
        }
        data.setBeginTime(beginTime);
        data.setEndTime(endTime);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        caTimesService.saveOrUpdate(data);
        return R.ok();
    }

    @ApiOperation(value = "删除时间段")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        long l = caTimesService.dropBean(storeUid, id);
        if (l <= 0) return R.failDel();
        return R.ok();
    }

    @ApiOperation(value = "更新时间段的状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = caTimesService.updateEnabled(storeUid, id, enabled, staffUid, staffName);
        if (!l) return R.fail("更新状态失败");

        return R.ok();
    }

}